//
//  ComfortDialogueView.swift
//  MoodDiary
//
//  Created by 杨鸿 on 2021/12/20.
//

import UIKit

class ComfortDialogueView: UIView {
    
    // MARK: Typealias
    typealias DidTouchUpInside = () -> Void
    
    // MARK: - Properties

    private var didTouchUpInsideButton: DidTouchUpInside?

    let comfortImv: UIImageView = {
        let imv = UIImageView()
        imv.image = UIImage(named: "拥抱图标")
        imv.contentMode = .scaleAspectFit
        return imv
    }()
    
    let comfortTitle: UILabel = {
        let lb = UILabel()
        lb.text = "别流泪，美好的生活总会不期而遇\n黎明即将来临"
        lb.numberOfLines = 2
        lb.textAlignment = .center
        lb.textColor = UIColor.black
        lb.font = UIFont(name: "PingFangSC-Medium", size: 16)
        return lb
    }()
    
    let button: UIButton = {
        let btn = UIButton()
        btn.setTitle("好的", for: .normal)
        btn.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 16)
        btn.setTitleColor(.black, for: .normal)
        btn.backgroundColor = .white
        btn.layer.borderWidth = 1.0
        btn.layer.borderColor = UIColor.black.cgColor
        btn.layer.cornerRadius = 19.0
        return btn
    }()
    
    // MARK: - Initializer

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.commonInit()
    }
    
    let comfortTextArr = ["快乐，不是永恒的；痛苦，也不是永恒的",
                          "把你的脸迎向阳光，那就不会有阴影",
                          "不要拿别人的错误惩罚自己",
                          "人生的真理，只是藏在平淡无味之中",
                          "时间总会过去的，让时间流走你的烦恼吧",
                          "昨日已经过去，而明天还没有来到，今天才是真实的",
                          "昨日已经过去，而明天还没有来到，今天才是真实的"]
    
    func commonInit() {
        
        addSubview(comfortImv)
        addSubview(comfortTitle)
        addSubview(button)
        comfortTitle.text = comfortTextArr[Int.random(in: 0..<comfortTextArr.count)]
        
        button.addTarget(self, action: #selector(tapButton), for: .touchUpInside)
        
        comfortImv.snp.makeConstraints { (maker) in
            maker.width.equalTo(94.0)
            maker.height.equalTo(80.0)
            maker.top.equalToSuperview().offset(40.0)
            maker.centerX.equalToSuperview()
        }
        
        comfortTitle.snp.makeConstraints { (maker) in
            maker.leading.equalToSuperview().offset(48.0)
            maker.trailing.equalToSuperview().offset(-48.0)
            maker.top.equalTo(comfortImv.snp.bottom).offset(20.0)
        }
        
        button.snp.makeConstraints { (maker) in
            maker.top.equalTo(comfortTitle.snp.bottom).offset(28.0)
            maker.centerX.equalToSuperview()
            maker.height.equalTo(38.0)
            maker.width.equalTo(95.0)
        }
    }
    
    @objc func tapButton() {
        self.didTouchUpInsideButton?()
    }
    
    public func didTouchUpInside(action: @escaping DidTouchUpInside) {
        self.didTouchUpInsideButton = action
    }
}
